package sql2005;

import java.sql.*;

public class ConnectToMSSQL2005 {
	public static void main(String[] args) {
		try {
			// load driver
//			 Class.forName("net.sourceforge.jtds.jdbc.Driver");
			 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//			System.out.println("driver da duoc load ");
			// tao ket noi den CSDL
//			 Connection connection = DriverManager.getConnection(
//			 "jdbc:jtds:sqlserver://localhost:1433/test", "sa",
//			 "sa");
			Connection connection = DriverManager
					.getConnection("jdbc:sqlserver://localhost:1433;databaseName=test;user=sa;password=sa");
//			System.out.println("Da ket noi duoc voi CSDL");
			// tao cau truy van
//			String sql = "SELECT * FROM LopHoc WHERE tenLH LIKE ?";
			String sql = "SELECT * FROM SinhVIen";
			// thuc hien cau truy van
			Statement state = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
			
			// ket qua tra ve la tap hop cac dong
			ResultSet result = state.executeQuery(sql);
			
			
			result.absolute(3);
			result.deleteRow();
			
			
			//state.executeUpdate("INSERT INTO SINHVIEN VALUES(777, 'ten 7777')");

			ResultSetMetaData data = result.getMetaData();
			int column = data.getColumnCount();
			for (int i = 1; i < column; i++) {
				 System.out.print(data.getColumnLabel(i)+"\t");				 
			}
			
			System.out.println();
			while (result.next()) {
				System.out.println(result.getString(1) + "\t"
						+ result.getString(2) + "\t");
			}


			result.close();
			state.close();
			connection.close();
		} catch (ClassNotFoundException ex) {
			ex.printStackTrace();
		} catch (SQLException ex) {
			ex.printStackTrace();
		}
		

	}
}
